﻿@model NewsletterBoxModel
@using Nop.Web.Framework;
@using Nop.Web.Models.Newsletter;
<div class="footbox newsletter">
    <h3 class="title">@T("Newsletter.Title")</h3>
    <div id="newsletter-subscribe-block" class="newsletter-subscribe-block">
        @Html.TextBoxFor(x => x.NewsletterEmail, new { id = "newsletter-email", @class = "newsletter-text" })
        @Html.ValidationMessageFor(x => x.NewsletterEmail)
                <input type="button" value="" id="newsletter-subscribe-button" class="newsletter-subscribe-button" />
        <span id="subscribe-loading-progress" class="please-wait" style="display: none;">@T("Common.Wait...")
        </span>    
    </div>
    <div id="newsletter-result-block" class="newsletter-result-block">
    </div>
</div>

<script type="text/javascript">
        $(document).ready(function () {
            $('#newsletter-subscribe-button').click(function () {
                
                var email = $("#newsletter-email").val();
                var subscribeProgress = $("#subscribe-loading-progress");
                subscribeProgress.show();
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@(Url.RouteUrl("SubscribeNewsletter"))",
                    data: { "email": email },
                    success: function (data) {
                        subscribeProgress.hide();
                        $("#newsletter-result-block").html(data.Result);
                         if (data.Success) {
                            $('#newsletter-subscribe-block').hide();
                            $('#newsletter-result-block').show();
                         }
                         else {
                            $('#newsletter-result-block').fadeIn("slow").delay(2000).fadeOut("slow");
                         }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to subscribe.');
                        subscribeProgress.hide();
                    }  
                });                
                return false;
            });
        });
    </script>